// mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRule: false, //是否显示酒币规则
    // banner: 'http://img5.imgtn.bdimg.com/it/u=3381379707,3093310680&fm=26&gp=0.jpg',
    // my_img:'',
    avatar: 'http://img2.imgtn.bdimg.com/it/u=1533699999,2683378103&fm=26&gp=0.jpg',
    name: '红豆稀饭',
    integral: '0',
    active_nav: '0', //高亮的按钮
    nav: [
      {
        text: '购物车',
        url: '../../image/cart-icon.png',
        nav: '../cart/cart',
        color: 'fda5a5'
      },
      {
        text: '订单',
        nav: '../my_order/my_order',
        url: '../../image/order-icon.png',
        color: 'd6d1fe'
      }, {
        text: '酒币',
        nav: '../integral/integral',
        url: '../../image/integral-icon.png',
        color: 'c8f8c1'
      }, {
        text: '优惠券',
        url: '../../image/coupon-icon.png',
        nav: '../coupon/coupon',
        color: 'ffdba7'
      }, {
        text: '收货地址',
        url: '../../image/address-icon.png',
        nav: '../address/address',
        color: 'ffb1e1'
      }
    ],
    note: [],
    journal: [],
    note_height: '',
    journal_height: ''

  },
  nav_click(e) {
    this.setData({
      active_nav: e.target.dataset.id
    })
  },
  swiper(e) {
    this.setData({
      active_nav: parseInt(e.detail.current)
    })
    if (e.detail.current == 0) {
      this.setHeight('.note', 'note_height')
    } else {
      this.setHeight('.journal', 'note_height')
    }
  },
  //重置高度  name节点名字 data_name重置的数据的名字
  setHeight(name, data_name) {
    var height
    var dome = wx.createSelectorQuery().select(name);
    let self = this
    dome.fields({ size: true }).exec(res => {
      this.setData({
        [data_name]: res[0].height
      })
      console.log(res[0])
    })
  },
  closeMsk(e) {
    // 验证是否已读酒币规则
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this
    wx.request({
      url: baseUrl + '/Journal/is_read',
      method: 'GET',
      data: { access_token },
      success: data => {
        console.log(data);
        console.log(e.target.dataset.name)
        if (e.target.dataset.name == 'msk' && data.data.code == 200) {
          this.setData({
            showRule: false
          })
        }

      }
    });

  },
  getData() {
    wx.showLoading({
      title: '正在加载...',
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this
    wx.request({
      url: baseUrl + '/Journal/index',
      data: { access_token },
      method: 'GET',
      success: data => {
        console.log(data.data.data)
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
        if (data.data.code == 200) {
          self.setData({
            data: data.data.data
          }, function () {
            self.setHeight('.node_nodata', 'note_height')
          })

          if (data.data.data.userInfo.is_read == 2) {
            // 酒币规则
            wx.request({
              url: baseUrl + '/Integral/index',
              method: 'POST',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              data: { access_token },
              success: data => {
                wx.hideLoading()

                self.setData({
                  rule: data.data.data,
                  showRule: true
                })
              }
            });
          } else {
            self.setData({
              showRule: false
            })
          }
        }
      }
    });

  },
  // 删除确认
  del(e) {
    let self = this
    wx.showModal({
      content: '确认删除吗？',
      confirmColor: '#9b0000',
      success: function (res) {
        console.log(res)
        res.confirm ? self.del_(e.currentTarget.dataset.id) : ''
      }
    })
  },
  // 删除
  del_(note_id) {
    wx.showLoading({
      title: '正在删除...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this,
      header = getApp().globalData.header
    wx.request({
      url: baseUrl + '/Journal/note_delete',
      data: { access_token, note_id },
      method: 'post',
      header,
      success: data => {
        wx.hideLoading()
        if (data.data.code == 200) {
          wx.showToast({
            title: '删除成功！',
          })
          setTimeout(function () {
            self.getData()
          }, 1000)
        }else{
          wx.showToast({
            title: data.data.message,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    if (!getApp().globalData.access_token) {
      getApp().setUser(function () {
        return self.getData()
      })
    } else {
      self.getData()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.data.active_nav == 0 ? this.setHeight('.note', 'note_height') : this.setHeight('.journal', 'note_height')
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})